# -*- coding: utf-8 -*-
"""
@Time ： 2023/2/17 10:34
@Auth ： 大雄
@File ：vk_code.py
@IDE ：PyCharm
@Email:3475228828@qq.com
@func:功能
"""
import ctypes

MapVirtualKey = ctypes.windll.user32.MapVirtualKeyW
# MapVirtualKey Map Types
MAPVK_VK_TO_CHAR = 2
MAPVK_VK_TO_VSC = 0
MAPVK_VSC_TO_VK = 1
MAPVK_VSC_TO_VK_EX = 3
# event
VK_CODE = {
        'backspace': 0x08,
        'tab': 0x09,
        'clear': 0x0C,
        'enter': 0x0D,
        'shift': 0x10,
        'ctrl': 0x11,
        'alt': 0x12,
        'pause': 0x13,
        'caps_lock': 0x14,
        'esc': 0x1B,
        'spacebar': 0x20,
        'page_up': 0x21,
        'page_down': 0x22,
        'end': 0x23,
        'home': 0x24,
        'left_arrow': 0x25,
        'up_arrow': 0x26,
        'right_arrow': 0x27,
        'down_arrow': 0x28,
        'select': 0x29,
        'print': 0x2A,
        'execute': 0x2B,
        'print_screen': 0x2C,
        'ins': 0x2D,
        'del': 0x2E,
        'help': 0x2F,
        '0': 0x30,
        '1': 0x31,
        '2': 0x32,
        '3': 0x33,
        '4': 0x34,
        '5': 0x35,
        '6': 0x36,
        '7': 0x37,
        '8': 0x38,
        '9': 0x39,
        'a': 0x41,
        'b': 0x42,
        'c': 0x43,
        'd': 0x44,
        'e': 0x45,
        'f': 0x46,
        'g': 0x47,
        'h': 0x48,
        'i': 0x49,
        'j': 0x4A,
        'k': 0x4B,
        'l': 0x4C,
        'm': 0x4D,
        'n': 0x4E,
        'o': 0x4F,
        'p': 0x50,
        'q': 0x51,
        'r': 0x52,
        's': 0x53,
        't': 0x54,
        'u': 0x55,
        'v': 0x56,
        'w': 0x57,
        'x': 0x58,
        'y': 0x59,
        'z': 0x5A,
        'numpad_0': 0x60,
        'numpad_1': 0x61,
        'numpad_2': 0x62,
        'numpad_3': 0x63,
        'numpad_4': 0x64,
        'numpad_5': 0x65,
        'numpad_6': 0x66,
        'numpad_7': 0x67,
        'numpad_8': 0x68,
        'numpad_9': 0x69,
        'multiply_key': 0x6A,
        'add_key': 0x6B,
        'separator_key': 0x6C,
        'subtract_key': 0x6D,
        'decimal_key': 0x6E,
        'divide_key': 0x6F,
        'f1': 0x70,
        'f2': 0x71,
        'f3': 0x72,
        'f4': 0x73,
        'f5': 0x74,
        'f6': 0x75,
        'f7': 0x76,
        'f8': 0x77,
        'f9': 0x78,
        'f10': 0x79,
        'f11': 0x7A,
        'f12': 0x7B,
        'f13': 0x7C,
        'f14': 0x7D,
        'f15': 0x7E,
        'f16': 0x7F,
        'f17': 0x80,
        'f18': 0x81,
        'f19': 0x82,
        'f20': 0x83,
        'f21': 0x84,
        'f22': 0x85,
        'f23': 0x86,
        'f24': 0x87,
        'num_lock': 0x90,
        'scroll_lock': 0x91,
        'left_shift': 0xA0,
        'right_shift ': 0xA1,
        'left_control': 0xA2,
        'right_control': 0xA3,
        'left_menu': 0xA4,
        'right_menu': 0xA5,
        'browser_back': 0xA6,
        'browser_forward': 0xA7,
        'browser_refresh': 0xA8,
        'browser_stop': 0xA9,
        'browser_search': 0xAA,
        'browser_favorites': 0xAB,
        'browser_start_and_home': 0xAC,
        'volume_mute': 0xAD,
        'volume_Down': 0xAE,
        'volume_up': 0xAF,
        'next_track': 0xB0,
        'previous_track': 0xB1,
        'stop_media': 0xB2,
        'play/pause_media': 0xB3,
        'start_mail': 0xB4,
        'select_media': 0xB5,
        'start_application_1': 0xB6,
        'start_application_2': 0xB7,
        'attn_key': 0xF6,
        'crsel_key': 0xF7,
        'exsel_key': 0xF8,
        'play_key': 0xFA,
        'zoom_key': 0xFB,
        'clear_key': 0xFE,
        '+': 0xBB,
        ',': 0xBC,
        '-': 0xBD,
        '.': 0xBE,
        '/': 0xBF,
        '`': 0xC0,
        ';': 0xBA,
        '[': 0xDB,
        '\\': 0xDC,
        ']': 0xDD,
        "'": 0xDE}
# SendInput
VK_CODE2 = {
        'escape': 0x01,
        'esc': 0x01,
        'f1': 0x3B,
        'f2': 0x3C,
        'f3': 0x3D,
        'f4': 0x3E,
        'f5': 0x3F,
        'f6': 0x40,
        'f7': 0x41,
        'f8': 0x42,
        'f9': 0x43,
        'f10': 0x44,
        'f11': 0x57,
        'f12': 0x58,
        'printscreen': 0xB7,
        'prntscrn': 0xB7,
        'prtsc': 0xB7,
        'prtscr': 0xB7,
        'scrolllock': 0x46,
        'pause': 0xC5,
        '`': 0x29,
        '1': 0x02,
        '2': 0x03,
        '3': 0x04,
        '4': 0x05,
        '5': 0x06,
        '6': 0x07,
        '7': 0x08,
        '8': 0x09,
        '9': 0x0A,
        '0': 0x0B,
        '-': 0x0C,
        '=': 0x0D,
        'backspace': 0x0E,
        'insert': 0xD2 + 1024,
        'home': 0xC7 + 1024,
        'pageup': 0xC9 + 1024,
        'pagedown': 0xD1 + 1024,
        # numpad
        'numlock': 0x45,
        'divide': 0xB5 + 1024,
        'multiply': 0x37,
        'subtract': 0x4A,
        'add': 0x4E,
        'decimal': 0x53,
        'numpadenter': 0x9C + 1024,
        'numpad1': 0x4F,
        'numpad2': 0x50,
        'numpad3': 0x51,
        'numpad4': 0x4B,
        'numpad5': 0x4C,
        'numpad6': 0x4D,
        'numpad7': 0x47,
        'numpad8': 0x48,
        'numpad9': 0x49,
        'numpad0': 0x52,
        # end numpad
        'tab': 0x0F,
        'q': 0x10,
        'w': 0x11,
        'e': 0x12,
        'r': 0x13,
        't': 0x14,
        'y': 0x15,
        'u': 0x16,
        'i': 0x17,
        'o': 0x18,
        'p': 0x19,
        '[': 0x1A,
        ']': 0x1B,
        '\\': 0x2B,
        'del': 0xD3 + 1024,
        'delete': 0xD3 + 1024,
        'end': 0xCF + 1024,
        'capslock': 0x3A,
        'a': 0x1E,
        's': 0x1F,
        'd': 0x20,
        'f': 0x21,
        'g': 0x22,
        'h': 0x23,
        'j': 0x24,
        'k': 0x25,
        'l': 0x26,
        ';': 0x27,
        "'": 0x28,
        'enter': 0x1C,
        'return': 0x1C,
        'shift': 0x2A,
        'shiftleft': 0x2A,
        'z': 0x2C,
        'x': 0x2D,
        'c': 0x2E,
        'v': 0x2F,
        'b': 0x30,
        'n': 0x31,
        'm': 0x32,
        ',': 0x33,
        '.': 0x34,
        '/': 0x35,
        'shiftright': 0x36,
        'ctrl': 0x1D,
        'ctrlleft': 0x1D,
        'win': 0xDB + 1024,
        'winleft': 0xDB + 1024,
        'alt': 0x38,
        'altleft': 0x38,
        ' ': 0x39,
        'space': 0x39,
        'altright': 0xB8 + 1024,
        'winright': 0xDC + 1024,
        'apps': 0xDD + 1024,
        'ctrlright': 0x9D + 1024,
        # arrow key scancodes can be different depending on the hardware,
        # so I think the best solution is to look it up based on the virtual key
        # https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeya?redirectedfrom=MSDN
        'up': MapVirtualKey(0x26, MAPVK_VK_TO_VSC),
        'left': MapVirtualKey(0x25, MAPVK_VK_TO_VSC),
        'down': MapVirtualKey(0x28, MAPVK_VK_TO_VSC),
        'right': MapVirtualKey(0x27, MAPVK_VK_TO_VSC),
}
def vk_to_char(key_str:str,type_=VK_CODE):
    return type_[key_str.lower()]


